package com.shopcart.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopcart.controller.CartController;
import com.shopcart.controller.CustomerController;
import com.shopcart.controller.ItemController;
import com.shopcart.entity.Customer;
import com.shopcart.exception.CustomerIsLoginedException;
import com.shopcart.exception.NoSuchCustomerExistsException;
import com.shopcart.exception.PasswordMismatchException;

@WebServlet(name = "LoginServlet", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		if (session.getAttribute("customer") != null) {
			req.getRequestDispatcher("home.jsp").forward(req, resp);
			return;
		} else {
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CustomerController customerContoller = null;
		ItemController itemController = null;
		CartController cartController = null;
		
		try {
			Context ctx = new InitialContext();
			customerContoller = (CustomerController) ctx.lookup("ejb/CustomerControllerBean");
			itemController = (ItemController) ctx.lookup("ejb/ItemControllerBean");
			cartController = (CartController) ctx.lookup("ejb/CartControllerBean");
		} catch (NamingException e) {
			System.err.println("Error getting bean!");
			e.printStackTrace();
		}
		
		if (request.getSession().getAttribute("customer") != null) {
			request.setAttribute("error", "You have to log out in order to login");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}

		String email = request.getParameter("email");
		String password = request.getParameter("password");

		try {
			Customer c = customerContoller.loginCustomer(email, password);
			HttpSession session = request.getSession();
			session.setAttribute("customer", c);
			session.setAttribute("itemController", itemController);
			cartController.setCustomer(c);
			session.setAttribute("cartController", cartController);
			request.getRequestDispatcher("home.jsp").forward(request, response);
			
			return;
		} catch (NoSuchCustomerExistsException e) {
			System.err.println(e.getMessage());
		} catch (CustomerIsLoginedException e) {
			System.err.println(e.getMessage());
		} catch (PasswordMismatchException e) {
			System.err.println(e.getMessage());
		} finally {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
}